<?php require('connections/con_gl.php'); ?>
<?php
cekAkses($_SESSION[akses],"PJ-6");
$currentPage = $_SERVER["PHP_SELF"];

$maxRows_data = 8;
$pageNum_data = 0;
if (isset($_GET['pageNum_data'])) {
  $pageNum_data = $_GET['pageNum_data'];
}
$startRow_data = $pageNum_data * $maxRows_data;
mysql_select_db($database_con_gl, $con_gl);
$query_data = "SELECT distinct gl_rtrans.no_ref, gl_rtrans.tgl,  gl_trans.tglbayar, gl_trans.x_ref, gl_trans.tglretur, gl_trans.retur, gl_trans.transaksi, gl_trans.jumlah, gl_trans.hargasatuan, gl_trans.diskon, gl_trans.total, gl_trans.barang, gl_trans.tglretur, gl_rtrans.jenis FROM gl_rtrans, gl_trans WHERE gl_rtrans.kontak='$_GET[kontak]' AND gl_trans.no_ref=gl_rtrans.no_ref AND ((gl_rtrans.jenis='7' AND gl_trans.pos='K') OR (gl_rtrans.jenis='3' AND gl_trans.pos='K' AND gl_trans.transaksi LIKE 'Pembayaran%'))";
if(!strstr($_SESSION[akses],'A-1-4')) {
	$query_data .= " AND gl_rtrans.gudang='$_SESSION[lokasi]'";
}
if($_GET[mulai]<>'' AND $_GET[sampai]<>'') {
$query_data .=" AND gl_rtrans.tgl between '$_GET[mulai]' AND '$_GET[sampai]'";
}
$query_data .=" ORDER BY gl_rtrans.tgl, gl_trans.id";
$data = mysql_query($query_data, $con_gl) or die(mysql_error());
$row_data = mysql_fetch_assoc($data);
$totalRows_data = mysql_num_rows($data);

//echo $query_data;
$queryString_data = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_data") == false && 
        stristr($param, "totalRows_data") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_data = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_data = sprintf("&totalRows_data=%d%s", $totalRows_data, $queryString_data);

mysql_select_db($database_con_gl, $con_gl);
$query_agen = "SELECT gl_kontak.id, gl_kontak.nama, gl_kontak.type FROM gl_kontak WHERE gl_kontak.type ='Customer' ORDER BY gl_kontak.nama";
$agen = mysql_query($query_agen, $con_gl) or die(mysql_error());
$row_agen = mysql_fetch_assoc($agen);
$totalRows_agen = mysql_num_rows($agen);

mysql_select_db($database_con_gl, $con_gl);
$query_getagen = "SELECT gl_kontak.id, gl_kontak.nama FROM gl_kontak WHERE gl_kontak.id='$_GET[kontak]'";
$getagen = mysql_query($query_getagen, $con_gl) or die(mysql_error());
$row_getagen = mysql_fetch_assoc($getagen);
$totalRows_getagen = mysql_num_rows($getagen);
include('include/widget_exportdata.php');
?>
<link href="css/ui.datepicker.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="js/ui.datepicker.js"></script>
<script type="text/javascript">
<!--
$(function()
      {
        $('.calendar').datepicker({
            appendText : "",
            dateFormat : 'yy/mm/dd'
          });
      });
</script>
<h1>Kartu Piutang</h1>
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" class="datatable"> 
  <tr align="right" class="hide">
    <td colspan="11" style="border-bottom:none;"><form id="search" name="search" method="post" action="">
      <label>
      <select name="agen" id="agen">
        <?php
do {  
?>
        <option value="<?php echo $row_agen['id']?>"><?php echo $row_agen['nama']?></option>
        <?php
} while ($row_agen = mysql_fetch_assoc($agen));
  $rows = mysql_num_rows($agen);
  if($rows > 0) {
      mysql_data_seek($agen, 0);
	  $row_agen = mysql_fetch_assoc($agen);
  }
?>
      </select>
      </label>
<label>
<input name="mulai" type="text" id="mulai" value="<?php echo $_REQUEST['mulai'];?>" size="12" maxlength="12" class="calendar";/>
      </label>s/d
	  <label>
      <input name="sampai" type="text" id="sampai" value="<?php echo $_REQUEST['sampai'];?>" size="12" maxlength="12" class="calendar"/>
	  </label>
      <label>
      <input name="Search" type="submit" id="Search" value="Search" />
      </label>
    </form>
	<?php
	if(isset($_POST['Search'])) {
		echo "<script>window.location=\"index.php?component=kartupiutang&kontak=".$_POST[agen]."&mulai=".$_POST[mulai]."&sampai=".$_POST[sampai]."\";</script>";
	}
	?>	</td>
  </tr>
  <?php if(isset($_GET[kontak]) AND $_GET[kontak]<>'') { ?>
  <tr>
    <td colspan="11">NAMA AGEN : <strong><?php echo $row_getagen['nama']; ?></strong></td>
  </tr>
  <tr>
    <th><strong>Tanggal</strong></th>
    <th><strong>Referensi</strong></th>
    <th><strong>Transaksi</strong></th>
    <th><strong>Barang</strong></th>
    <th align="center"><strong>Jumlah</strong></th>
    <th align="center"><strong>Harga</strong></th>
    <th align="center"><strong>Diskon(%)</strong></th>
    <th align="center"><strong>Piutang</strong></th>
    <th align="center"><strong>Pembayaran</strong></th>
    <th align="center"><strong>Retur</strong></th>
    <th align="center"><strong>Saldo</strong></th>
  </tr>
  <?php if($totalRows_data > 0) { ?>
  <?php $saldo = 0; do { ?>
  <?php
mysql_select_db($database_con_gl, $con_gl);
$query_hitung = "SELECT sum(gl_trans.total) FROM gl_trans WHERE gl_trans.no_ref='$row_data[no_ref]' AND pos='K'";
$hitung = mysql_query($query_hitung, $con_gl) or die(mysql_error());
$row_hitung = mysql_fetch_assoc($hitung);
$totalRows_hitung = mysql_num_rows($hitung);

mysql_select_db($database_con_gl, $con_gl);
$query_getbrg = "SELECT barang FROM gl_barang where id='$row_data[barang]'";
$getbrg = mysql_query($query_getbrg, $con_gl) or die(mysql_error());
$row_getbrg = mysql_fetch_assoc($getbrg);
$totalRows_getbrg = mysql_num_rows($getbrg);
  ?>
  <?php //if(!strstr($row_data['transaksi'],"Piutang") AND !strstr($row_data['transaksi'],"Penjualan")) { ?>
    <tr valign="top" bgcolor="#FFFFFF">
      <td width="54" ><?php if(isset($row_data[tglbayar])) { tanggal($row_data['tglbayar'],"tampilkan");}else if(isset($row_data[tglretur]) AND $row_data[tglretur]<>0) { tanggal($row_data['tglretur'],"tampilkan");}else{ tanggal($row_data['tgl'],"tampilkan");} ?></td>
      <td width="77" ><a href="include/jurnalvoucher_transaksi.php?referensi=<?php echo $row_data['no_ref'];?>" onclick="NewWindow(this.href,'name','1050','300','yes');return false" title="Jurnal Voucher"><?php echo $row_data['no_ref']; ?></a></td>
      <td width="167" ><?php echo $row_data['transaksi'];?></td>
      <td width="113" ><?php if($row_getbrg['barang']<>'') { echo $row_getbrg['barang'];}else{ echo "&nbsp;";} ?></td>
      <td width="48" align="right" ><?php $jum = $row_data['jumlah']; if($jum<>'') { echo $jum;}else{ echo "&nbsp;";}?></td>
      <td width="50" align="right" ><?php $hrgsat = number_format($row_data['hargasatuan'],0,',','.').",-"; echo $hrgsat;?></td>
      <td width="90" align="center" bgcolor="#FFFFFF" ><?php $diskon = $row_data['diskon']; if($diskon<>'') { echo $diskon;}else{ echo "&nbsp;";}?></td>
      <td width="72" align="right" bgcolor="#FFFFFF" ><?php if(substr($row_data[no_ref],0,2)=='SJ') { ?>
        <?php echo number_format($row_data['total'],0,',','.').",-"; $saldo +=$row_data['total'];?>
        <?php }else{ ?>
        -
      <?php } ?></td>
      <td width="83" align="right" bgcolor="#FFFFFF" ><?php if(strstr($row_data['no_ref'],"CR") OR $row_data['x_ref']<>'') { ?>
        <?php echo number_format($row_data['total'],0,',','.').",-"; $saldo -=$row_data['total'];?>
      <?php }else{ ?>-<?php } ?></td>
      <td width="72" align="right" bgcolor="#FFFFFF" ><?php if(substr($row_data[no_ref],0,2)=='SR') { ?><?php echo number_format($row_data['total'],0,',','.').",-"; $saldo -=$row_data['total'];?><?php }else{ ?>-<?php } ?></td>
      <td width="70" align="right" bgcolor="#FFFFFF" ><?php echo number_format($saldo,0,',','.').",-";?></td>
    </tr>
	<?php //} ?>
    <?php } while ($row_data = mysql_fetch_assoc($data)); ?>
    <tr class="hide">
      <td colspan="11"  ><a href="include/print_kartupiutang.php?kontak=<?php echo $_GET[kontak];?>&amp;mulai=<?php echo $_GET[mulai];?>&amp;sampai=<?php echo $_GET[sampai];?>" onclick="NewWindow(this.href,'name','1080','500','yes');return false"><img src="images/_print.png" border="0" /> Cetak</a></td>
    </tr>
	<?php if($totalRows_data > $maxRows_data) { ?>
    <tr class="hide">
      <td colspan="11"  ><table border="0" align="left">
          <tr>
            <td width="23%" align="center"><?php if ($pageNum_data > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, 0, $queryString_data); ?>"><img src="First.gif" border="0" /></a>
                <?php } // Show if not first page ?>            </td>
            <td width="31%" align="center"><?php if ($pageNum_data > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, max(0, $pageNum_data - 1), $queryString_data); ?>"><img src="Previous.gif" border="0" /></a>
                <?php } // Show if not first page ?>            </td>
            <td width="23%" align="center"><?php if ($pageNum_data < $totalPages_data) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, min($totalPages_data, $pageNum_data + 1), $queryString_data); ?>"><img src="Next.gif" border="0" /></a>
                <?php } // Show if not last page ?>            </td>
            <td width="23%" align="center"><?php if ($pageNum_data < $totalPages_data) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, $totalPages_data, $queryString_data); ?>"><img src="Last.gif" border="0" /></a>
                <?php } // Show if not last page ?>            </td>
          </tr>
        </table></td>
    </tr>
	<?php } ?>
   <?php }else{ ?>
  <tr bgcolor="#FFFFFF">
    <td colspan="11"  >Data tidak ada !!! </td>
  </tr>
  <?php } ?>
  <?php } ?>
</table>
